#include <iostream>
#include <fstream>
#include <cstdlib>
#include "emp.h"
#include "readfile.h"
using namespace std;

const int MAX = 20;
abstr_emp * pc[MAX];
int ct = 0;

void ShowMenu();
void ShowData();
void AddOne(fstream & f);
int main()
{
    fstream f("emloyee.dat",ios_base::in | ios_base::out | ios_base::app);
    if(f.is_open())
    {
        readfile(f);
        ShowMenu();
        char ch;
        while((ch = cin.get()) != 'Q' && ch != 'q')
        {
            switch(ch)
            {
            case 'A':
            case 'a': ShowData(); break;
            case 'B':
            case 'b': AddOne(f); break;
            default:
                cout << "Error! Please enter A, B, or Q! \n";
                break;
            }
            while((ch = cin.get()) != '\n') continue;
            ShowMenu();
        }
    }
    else
    {
        cerr << "can't open the file.\n";
        exit(EXIT_FAILURE);
    }

    for(int i = 0; i < ct; i++)
        delete pc[i];
    return 0;
}


void ShowMenu()
{
    cout << "Menu: \n";
    cout << "A:Show All Emloyee\tB: Add Emloyee\n";
    cout << "Q: Quit\n";
    cout << endl;
    cout << "Please enter a character: ";
}

void ShowData()
{
    cout << "Here are the current contents of the " << "temloyee.dat" << " file:\n";
    for(int i = 0; i < ct; i++)
    {
        pc[i]->ShowAll();
        cout << endl;
    }
    cout << endl;
}

void AddOne(fstream & f)
{
    if(f.eof()) f.clear();
    f.seekp(0, ios_base::end);
    cout << "There has some classtype: \n";
    cout << "1: Employee \t2: Manager\n";
    cout << "3: Fink \t4: Highfink\n";
    cout << "Please choose a type to create: ";
    char ch;
    cin >> ch;
    switch(ch)
    {
        case '1' : pc[ct] = new employee; pc[ct]->SetAll(); f << "1 "; pc[ct]->WriteAll(f); ct++; break;
        case '2' : pc[ct] = new manager; pc[ct]->SetAll(); f << "2 "; pc[ct]->WriteAll(f); ct++; break;
        case '3' : pc[ct] = new fink; pc[ct]->SetAll(); f << "3 "; pc[ct]->WriteAll(f); ct++; break;
        case '4' : pc[ct] = new highfink; pc[ct]->SetAll(); f << "4 "; pc[ct]->WriteAll(f); ct++; break;
        default : cout << "Error number!\n"; break;
    }

}
